package micro.sgs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MicroSGSActivity extends Activity {
    /** Called when the activity is first created. */
	
	Button doorOpener;
	Button eventAdder;
	String phoneStr;
	public static final String PREFS_NAME = "ACC";
   
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        
        //boolean firstime = settings.getBoolean("firstime",true);
        int firstime = settings.getInt("first",1);
        phoneStr=settings.getString("usrPhone", "0");
        
        setContentView(R.layout.main);
        
        if(firstime==1){
    		
    		Context mContext = getApplicationContext();
    		Dialog dialog = new Dialog(mContext);

    		//dialog.setContentView(R.layout.firstime);
    		//dialog.setTitle("Custom Dialog");
    		
    		//LayoutInflater factory = LayoutInflater.from(this);
    		//final View textEntryView = factory.inflate(R.layout.firstime, null);
    		
    		final EditText input = new EditText(this);
    		input.setRawInputType(3);
    		
    		 new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("New Resident").setMessage("Enter Resident's Phone Number").setView(input).setPositiveButton(
    	                "Submit", new DialogInterface.OnClickListener() {
    	                    public void onClick(DialogInterface dialog, int which) {
    	      	             	phoneStr = input.getText().toString();
    	      	                dialog.dismiss();
    	                    }
    	                }).show();
    		//dialog.show();

            SharedPreferences.Editor editor = settings.edit();
            //editor.putBoolean("firstime", false);
            editor.putInt("first", 0);
            editor.putString("usrPhone",phoneStr);

            // Commit the edits!
            editor.commit();
        }
        
                       
        doorOpener = (Button) findViewById(R.id.Open);
        eventAdder = (Button) findViewById(R.id.AddEvent);
        
    	doorOpener.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View v) {

				String phoneNo = "7874025592";
				String sms = phoneStr+"OPEN";

				try {
					SmsManager smsManager = SmsManager.getDefault();
					smsManager.sendTextMessage(phoneNo, null, sms, null, null);
					Toast.makeText(getApplicationContext(), "SMS Sent!",
							Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(),
							"SMS faild, please try again later!",
							Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}

			}
		});
    	
    	eventAdder.setOnClickListener(new OnClickListener(){
    		
    		public void onClick(View v){
    			
    			setContentView(R.layout.events);
    			
    			Button submit=(Button) findViewById(R.id.submit);
    			final EditText keycode=(EditText) findViewById(R.id.keycode);
    			
    			submit.setOnClickListener(new OnClickListener(){
    				
    				public void onClick(View v){
    					
    					String phoneNo = "7874025592";
    					String sms = phoneStr+"EVNT"+keycode.getText().toString();

    					try {
    						SmsManager smsManager = SmsManager.getDefault();
    						smsManager.sendTextMessage(phoneNo, null, sms, null, null);
    						Toast.makeText(getApplicationContext(), "SMS Sent!",
    								Toast.LENGTH_LONG).show();
    						setContentView(R.layout.main);
    					} catch (Exception e) {
    						Toast.makeText(getApplicationContext(),
    								"SMS faild, please try again later!",
    								Toast.LENGTH_LONG).show();
    						e.printStackTrace();
    					}
    					
    				}
    				
    			});
    		}
    	});
    	
    	
    }
}